var app=sm("do_App");
var page=sm("do_Page");
var nf=sm("do_Notification");
var global=sm("do_Global");
var cache = sm("do_DataCache");
var config=require("config");
var storage = sm("do_Storage");
var device = sm("do_Device");

var do_back=ui("do_back");
var do_exit=ui("do_exit");
var do_verson=ui("do_verson");
var do_update=ui("do_update");
var do_ALayout_root=ui("do_ALayout_root");

var isupdate=false;
var iosurl="";
var androidurl="";

do_ALayout_root.add("loadingUI", "source://view/loadingUI.ui", 0, 0);
var loadingUI = ui("loadingUI");
loadingUI.visible = false;	


do_back.on("touch",function(){
	app.closePage();
});
page.on("back",function(){
	app.closePage();
});
page.on("loaded",function(){
	var nowdate=new Date();
	var olddate=nowdate.getTime();
	app.fire("tologin",{times:olddate,pagesize:2});
	var getVerSion = global.getVersion().ver;
	var uhttp = mm("do_Http");
	uhttp.method="POST";
	uhttp.timeout = 30000;
	uhttp.contentType = "application/json";
	uhttp.url = config.baseUrl+"getAppUpdateInfo";
	uhttp.body = JSON.stringify({appId:app.getAppID(),platform:1,mobileVersion:getVerSion});
	uhttp.on("success", function(data, e) {
		loadingUI.visible = false;
		if(data.result==1 && data.data){
			if (data.data.isLatestVersion == "true") {  //是否对当前版本更新
				ui("do_version").visible=true;
				isupdate=true;
				iosurl=data.data.iosurl;
				androidurl=data.data.androidurl;
			}
			else{
				nf.toast("当前是最新版本");
			}
		}
		else{
			nf.toast(data.msg || "获取版本更新失败");
		}
		
	});
	uhttp.on("fail",function(data, e) {
		deviceone.print("错误"+JSON.stringify(data));
	});
	uhttp.request();	
});
do_verson.text=global.getVersion().ver;
do_exit.on("touch", function(){
	cache.saveData("userinfo", "");
	 app.closePage({data:"", animationType:"", layer:2});	 
	
}).on("touchDown", function(){
	do_exit.bgColor = "B10505FF";
}).on("touchUp", function(){
	do_exit.bgColor = "DE0707FF";
});
do_update.on("touch", function(){
	var nowdate=new Date();
	var olddate=nowdate.getTime();
	app.fire("tologin",{times:olddate,pagesize:2});
	if (isupdate) {  //是否对当前版本更新
		nf.confirm("有新版本，是否进行版本更新？","版本更新提示", "更新", "取消", function(index) {
			if(index=="1")
				{
				var appURL = (device.getInfo().OS=="android")?androidurl:iosurl;
				sm("do_External").openURL(appURL);
				}
		})
	}
	else{
		nf.toast("当前是最新版本");
	}
	
}).on("touchDown", function(){
	do_update.bgColor = "E0E0E0FF";
}).on("touchUp", function(){
	do_update.bgColor = "FFFFFFFF";
});